﻿package com.wandian.util.loader
{
	import com.wandian.data.GameData;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class LoadXML extends EventDispatcher
	{
		private var _xml:XML;
		private var _undefinedIndex:uint=0;
		private var _allXML:Object;
		private var _xmlName:String;
		private var _urlLoderCl:URLLoader;
		private var _XMLURLList:Array;
		private var _arrListIndex:int;
		private var _errorFunction:Function;
		private static var _This:LoadXML;
		public static const LOAD_XML_OK:String="loadXMLOK";

		public function LoadXML()
		{
			return;
		} 

		public function loadByURLList(urllist:Array):void
		{
			this._XMLURLList=urllist;
			this._arrListIndex=0;
			this._allXML=new Object();
			this.loadNextXML();
			return;
		} 

		private function loadByURL(url:String):void
		{
			trace(url);
			this._xmlName=this.getXmlFileName(url);
			this._urlLoderCl=new URLLoader();
			this._urlLoderCl.load(new URLRequest(url));
			this._urlLoderCl.addEventListener(IOErrorEvent.IO_ERROR, this.LoadXMLError);
			this._urlLoderCl.addEventListener(Event.COMPLETE, this.loadXMLComplete);
			return;
		} 

		private function LoadXMLError(event:Event):void
		{
			this.removeListener();
			if (this._errorFunction != null)
			{
				this._errorFunction();
			}
			return;
		} 

		private function loadNextXML():void
		{
			if (this._XMLURLList != null)
			{
			
				this.loadByURL(this._XMLURLList[this._arrListIndex] as String);
				this._arrListIndex+=1;
			}
			return;
		} 

		public function getXmlFileName(url:String):String
		{
			var start:int=url.lastIndexOf("/") + 1;
			var end:int=url.lastIndexOf(".");
			if (end != -1)
			{
				url=url.slice(start, end);
			}
			else
			{
				this._undefinedIndex+=1;
				url="undefined_" + this._undefinedIndex++;
			}
			return url;
		} 

		private function loadXMLComplete(event:Event):void
		{
			this.removeListener();
			this._xml=new XML(this._urlLoderCl.data);
			this._allXML[this._xmlName]=this._xml;
			if (this._arrListIndex >= this._XMLURLList.length)
			{
				this.dispatchEvent(new Event(LOAD_XML_OK));
			}
			else
			{
				this.loadNextXML();
			}
			return;
		} 

		private function removeListener():void
		{
			if (this._urlLoderCl != null)
			{
				this._urlLoderCl.removeEventListener(IOErrorEvent.IO_ERROR, this.LoadXMLError);
				this._urlLoderCl.removeEventListener(Event.COMPLETE, this.loadXMLComplete);
			}
			return;
		} 

		public function getXML(param1:String):XML
		{
			return this._allXML[param1] as XML;
		} 

		public function set erroeFunction(param1:Function):void
		{
			this._errorFunction=param1;
			return;
		} 

		public static function getInstance():LoadXML
		{
			if (_This == null)
			{
				_This=new LoadXML;
			}
			return _This;
		} 

	}
}
